#!/usr/bin/python
# -*- coding: utf-8 -*-

import pickle
import logging

logging.basicConfig(filename="debug.log", level=logging.DEBUG, filemode='w')

scores = {'A': 4, 'A-': 3.7, 'B+': 3.3, 'B': 3, 'B-': 2.7, 'C+': 2.3,
          'C': 2, 'C-': 1.7, 'D': 1.3, 'D-': 1, 'F': 0, 'P': 4}

version = 'v0.3.20100217'

def encrypt(pword):
    return 'S'.join(map(lambda x: '%2x'%ord(x), pword))

def decrypt(pword):
    return ''.join(map(lambda x: chr(int(x, 16)), pword.split('S')))

def letterToPoint(letter):
    if scores.has_key(letter):
        return scores[letter]
    else:
        return -1

def saveConf(pair, filename='user.pickle'):
    f = open(filename, 'w')
    (user, pword) = pair
    pickle.dump((user, encrypt(pword)), f)
    f.close()

def loadConf(filename='user.pickle'):
    try:
        f = open(filename, 'r')
        (user, pword) = pickle.load(f)
        pword = decrypt(pword)
        logging.debug('Load from pickle, (user, password) = ' + user + ',' + pword)
        f.close()
        return (user, pword)
    except:
        return ('', '')
